/***************************************************************************
 *  Copyright (c) 2005 Chih-Chung Chang                                    *
 *  Parts copyright (c) 2006 Michael Olson                                 *
 *  Copyright (C) 2007-2008 by Fehmi "Canavar" SAGLAM                      *
 *  canavar@fehmicans.net                                                  *
 *                                                                         *
 *  This file is part of sTerm.                                            *
 *                                                                         *
 *  sTerm is free software; you can redistribute it and/or modify          *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  sTerm is distributed in the hope that it will be useful,               *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#include "../include/font.h"
#include "../include/GP2X.h"

int FONT_WIDTH;
int FONT_HEIGHT;
unsigned char *font;

//----------------------------------------------------------------------------//
//    draw char                                                               //
//----------------------------------------------------------------------------//
void draw_char( SDL_Surface *screen,
                SDL_Surface *bg,
                SDL_Surface *sterm,
                int row,
                int col,
                unsigned char c,
                int bg_color,
                int fg_color,
                bool inverse )
{    
    int idx;
    int y = row * FONT_HEIGHT;
    int x = col * FONT_WIDTH;
        
    if(sterm != NULL)
      lock_surface(sterm);
    else {
      lock_surface(screen);
    }
      
    idx = c * FONT_HEIGHT;
    for(int j=0; j<FONT_HEIGHT; j++)
    {
      unsigned char v = *(font + idx +j);
      for(int k=0; k<FONT_WIDTH; k++)
      {
         if(sterm != NULL)
         {
          *(short *)((char *)sterm->pixels + sterm->pitch*(y+j) + (x+k)*2) = 
            (v&0x80)?
            (inverse?bg_color:fg_color):
            (inverse?fg_color:bg_color);
         }
         else {
          *(short *)((char *)screen->pixels + screen->pitch*(y+j) + (x+k)*2) = 
            (v&0x80)?
            (inverse?bg_color:fg_color):
            (inverse?fg_color:bg_color);
         }         
          v <<= 1;
      }
    }
    
    if(sterm != NULL)
    {
      unlock_surface(sterm);
      SDL_Rect target;
      SDL_Rect source;
      source.x = x;
      source.y = y;
      source.w = FONT_WIDTH;
      source.h = FONT_HEIGHT;
      target.x = source.x + GP2X_CLIENT_TOP_X;
      target.y = source.y + GP2X_CLIENT_TOP_Y;
      target.w = source.w;
      target.h = source.h;
      if(bg != NULL)
      {
         SDL_BlitSurface(bg, &target, screen, &target);
         SDL_BlitSurface(sterm, &source, screen, &target);         
      }
      SDL_UpdateRect(screen, target.x, target.y, target.w, target.h);
    }
    else {
      unlock_surface(screen);
      SDL_UpdateRect(screen, x, y, FONT_WIDTH, FONT_HEIGHT);
    }
}


//----------------------------------------------------------------------------//
//    draw char to screen
// * used for vkeyboard only (uses big font)
//----------------------------------------------------------------------------//
void draw_char( SDL_Surface *screen,                
                int row,
                int col,
                unsigned char c,
                int bg_color,
                int fg_color,
                bool inverse )
{    
    int idx;
    int y = row * BIG_FONT_HEIGHT;
    int x = col * BIG_FONT_WIDTH;
              
    lock_surface(screen);
    
    for(int j=0; j<BIG_FONT_HEIGHT; j++)
    {
      unsigned char v = big_font[c][j];
      for(int k=0; k<BIG_FONT_WIDTH; k++)
      {
          *(short *)((char *)screen->pixels + screen->pitch*(y+j) + (x+k)*2) = 
            (v&0x80)?
            (inverse?bg_color:fg_color):
            (inverse?fg_color:bg_color);
                 
          v <<= 1;
      }
    }
  
    unlock_surface(screen);
    SDL_UpdateRect(screen, x, y, BIG_FONT_WIDTH, BIG_FONT_HEIGHT);
}


//----------------------------------------------------------------------------//
//    draw char                                                               //
//----------------------------------------------------------------------------//
/*void draw_char_to_surface( SDL_Surface *surface,
                           int x,
                           int y,
                           unsigned char c,
                           int bg_color,
                           int fg_color
                         )
{            
    int idx;
    if(surface != NULL)
      return;
     
    lock_surface(surface);
      
    idx = c * FONT_HEIGHT;
    for(int j=0; j<FONT_HEIGHT; j++)
    {
      unsigned char v = *(font + idx +j);
      for(int k=0; k<FONT_WIDTH; k++)
      {
         *(short *)((char *)surface->pixels + surface->pitch*(y+j) + (x+k)*2) = 
            (v&0x80)? fg_color : bg_color;
                              
         v <<= 1;
      }
    }
    
    unlock_surface(surface);
}*/

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void clear_char(SDL_Surface *screen, SDL_Surface *bg, SDL_Surface *sterm, int row, int col, int bg_color)
{
    draw_char(screen, bg, sterm, row, col, ' ',  bg_color, 0, false);
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void lock_surface(SDL_Surface *screen)
{
    if(SDL_MUSTLOCK(screen))
    {
        if ( SDL_LockSurface(screen) < 0 ) {
            fprintf(stderr, "Unable to lock_surface\n");
            return;
        }
    }
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void unlock_surface(SDL_Surface *screen)
{
    if (SDL_MUSTLOCK(screen))
    {
        SDL_UnlockSurface(screen);
    }
}

